Skip to content

Followup fixes for #158#159

Open
YapingLi04 wants to merge 2 commits intocooperlees:mainfrom
YapingLi04:followup
Open

Followup fixes for #158#159
YapingLi04 wants to merge 2 commits intocooperlees:mainfrom
YapingLi04:followup

Conversation

@YapingLi04
Copy link
Contributor

This addresses @ikruglov's comments on #158.

@YapingLi04
Copy link
Contributor Author

Thanks for the review @ikruglov!

config: &crate::config::Config,
socket_path: &str,
) -> std::result::Result<SystemdUnitStats, Box<dyn std::error::Error + Send + Sync>> {
) -> anyhow::Result<SystemdUnitStats> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much nicer - I was going to look to clean these up / make thiserror custom errors ... might throw AI to double check later today

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses feedback from PR #158 by improving error handling and validation in the Varlink metrics collection code. The changes make the code more robust and maintainable by explicitly validating metric types before extraction and moving fallback logic to the appropriate architectural layer.

Changes:

  • Enhanced error handling with explicit type validation and warning logs for invalid metrics
  • Changed value extraction methods to panic on misuse, requiring proper validation before use
  • Moved D-Bus fallback logic from update_unit_stats to its callers for better separation of concerns
  • Updated metric names from snake_case to PascalCase to match systemd's metrics API format
  • Simplified VarlinkConfig to use derive Default

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/varlink/metrics.rs Changed value extraction methods to require validation and panic on misuse instead of silently defaulting
src/varlink_units.rs Added explicit type validation before metric extraction, updated metric names to PascalCase, removed D-Bus fallback
src/machines.rs Added D-Bus fallback logic for container metrics collection with proper error handling
src/lib.rs Added D-Bus fallback logic for host metrics collection with proper error handling
src/config.rs Simplified VarlinkConfig to use derive Default

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +87 to 118
"UnitLoadState" => {
if should_skip_unit(&object_name, config) {
return Ok(());
}
let value = metric.value_as_string("" /* default_value */);
if !metric.value().is_string() {
warn!(
"Metric {} has non-string value: {:?}",
metric.name(),
metric.value()
);
return Ok(());
}
let value = metric.value_as_string();
let load_state = match SystemdUnitLoadState::from_str(value) {
Ok(v) => v,
Err(_) => {
warn!(
"Metric {} has unrecognized value: {:?}",
metric.name(),
value
);
return Ok(());
}
};
let unit_state = stats
.unit_states
.entry(object_name.to_string())
.or_default();
unit_state.load_state =
SystemdUnitLoadState::from_str(value).unwrap_or(SystemdUnitLoadState::unknown);
unit_state.load_state = load_state;
unit_state.unhealthy =
is_unit_unhealthy(unit_state.active_state, unit_state.load_state);
}
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UnitLoadState case duplicates validation and parsing logic that already exists in the parse_metric_enum helper function. The UnitActiveState case (lines 71-86) uses parse_metric_enum correctly, but this case reimplements the same logic inline. Consider refactoring to use parse_metric_enum for consistency and to reduce code duplication.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants